package com.cmcc.driver.ui.main.adapter

import android.content.Context
import android.graphics.Color
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.cmcc.driver.R
import com.cmcc.driver.data.entity.Order
import com.cmcc.driver.databinding.ListItemOrderRecordBinding
import com.cmcc.shared.SdkManager
import com.cmcc.shared.utils.SpHelper
import com.cmcc.shared.utils.TextViewUtil.setTextViewByLimit
import com.cmcc.shared.utils.TextViewUtil.setTextViewValue

class OrderRecordAdapter : RecyclerView.Adapter<OrderRecordViewHolder>() {

    private lateinit var context: Context
    private val currentList = mutableListOf<Order>()
    private var itemClickListener: MyOrderItemClickListener? = null

    override fun getItemCount(): Int {
        return currentList.size
    }

    fun removeList() {
        currentList.clear()
//        notifyDataSetChanged()
    }

    fun addList(records: List<Order>) {
        currentList.addAll(records)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OrderRecordViewHolder {
        this.context = parent.context
        return OrderRecordViewHolder(
            ListItemOrderRecordBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: OrderRecordViewHolder, position: Int) {
        val model = currentList[position]

        setTextViewByLimit(
            holder.binding.orderType,
            model.type == 1,
            context.getString(R.string.driver_real_time_order),
            context.getString(R.string.driver_reservation_order)
        )


        var type = SpHelper.getParam(context, "Mode", "type", "1")

        //当前是夜间模式 设置成白天模式
        if (type == "1") {
            if (model?.type == 1) {
                holder.binding.orderType.setTextColor(ContextCompat.getColor(context, R.color.driver_tip_text_color))
            }else {
                holder.binding.orderType.setTextColor(ContextCompat.getColor(context, R.color.driver_colorBase))
            }
        }

        holder.binding.createTime.text = model.createTime
        holder.binding.direction.directionStart.text = model.startAddress
        holder.binding.direction.directionEnd.text = model.endAddress


        setTextViewByLimit(
            holder.binding.options.textView3,
            model?.type == 1,
            context.getString(R.string.time_real_time),
            context.getString(R.string.order_create_time, model?.startTime)
        )

        setTextViewValue(
            context,
            holder.binding.options.tunnel,
            R.string.order_tunnel,
            model?.tunnel?.name,
            context.getString(R.string.what_ever)
        )


//        setTextViewValue(
//            context,
//            holder.binding.options.tip,
//            R.string.order_tip,
//            model?.bountyFee,
//            "0.00"
//        )

        if(model.bountyFee==null){
            holder.binding.options.tip.text = context.resources.getString(R.string.driver_tip)+"HK\$ "+"0.00"
        }
        else{
            holder.binding.options.tip.text = context.resources.getString(R.string.driver_tip)+"HK\$ "+model.bountyFee.toString()+""
        }


        setTextViewValue(
            context,
            holder.binding.options.station,
            R.string.order_station,
            if (model.stationIndex.isNullOrEmpty() || model?.stationIndex == "0") null else model?.stationIndex,
            if (model.stationIndex.isNullOrEmpty() || model?.stationIndex == "0") context.getString(
                R.string.what_ever
            ) else null
        )

        setTextViewValue(
            context,
            holder.binding.options.remarks,
            R.string.order_remark,
            model?.remark,
            context.getString(R.string.none)
        )

        holder.binding.root.setOnClickListener(createClickListener(model))
    }

    private fun createClickListener(model: Order): View.OnClickListener {

        var listener = View.OnClickListener { view ->
            itemClickListener?.click(view, model)
        };
        listener
        return listener;
    }

    fun addItemClickListener(listener: MyOrderItemClickListener) {
        itemClickListener = listener
    }

}

class OrderRecordDiffCallback : DiffUtil.ItemCallback<Order>() {
    override fun areItemsTheSame(oldItem: Order, newItem: Order): Boolean {
        return oldItem == newItem
    }

    override fun areContentsTheSame(oldItem: Order, newItem: Order): Boolean {
        return oldItem == newItem
    }
}


interface MyOrderItemClickListener {
    fun click(view: View, model: Order)
}

class OrderRecordViewHolder(val binding: ListItemOrderRecordBinding) :
    RecyclerView.ViewHolder(binding.root) {
    fun bind() {

    }
}